// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <cantor/function/maths/abs.h>
#include <cantor/function/scalar_function.h>

namespace cantor::builtin {

    template<typename T>
   struct AbsImpl {
        T operator()(T t1) {
            if constexpr (std::is_unsigned_v<T>) {
                return t1;
            }
            return  t1 < 0 ? -t1 : t1;
        }
    };

    turbo::Status initialize_abs(FunctionRegistry * registry) {
        ScalarFunctionBuilder builder(registry);
        std::vector<std::string> names = {"a"};
        std::string description = "Returns the absolute value of x";
        std::string func_name = "abs";
        /// int8_t
        auto rs = builder.register_unary_scalar_function<int8_t, int8_t, AbsImpl<int8_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// int16_t
        rs = builder.register_unary_scalar_function<int16_t, int16_t, AbsImpl<int16_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// int32_t
        rs = builder.register_unary_scalar_function<int32_t, int32_t, AbsImpl<int32_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// int64_t
        rs = builder.register_unary_scalar_function<int64_t, int64_t, AbsImpl<int64_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// turbo::int128
        rs = builder.register_unary_scalar_function<turbo::int128, turbo::int128, AbsImpl<turbo::int128> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// uint8_t
        rs = builder.register_unary_scalar_function<uint8_t, uint8_t, AbsImpl<uint8_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// uint16_t
        rs = builder.register_unary_scalar_function<uint16_t, uint16_t, AbsImpl<uint16_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// uint32_t
        rs = builder.register_unary_scalar_function<uint32_t, uint32_t, AbsImpl<uint32_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// uint64_t
        rs = builder.register_unary_scalar_function<uint64_t, uint64_t, AbsImpl<uint64_t> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// turbo::uint128
        rs = builder.register_unary_scalar_function<turbo::uint128, turbo::uint128, AbsImpl<turbo::uint128> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// float
        rs = builder.register_unary_scalar_function<float, float, AbsImpl<float> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        /// float
        rs = builder.register_unary_scalar_function<double, double, AbsImpl<double> >(
            "abs", names, description);
        TURBO_RETURN_NOT_OK(rs);

        return turbo::OkStatus();
    }

}  // namespace cantor::builtin
